home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example5.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  4KB  |  152 lines

  1. /*
  2. **    example5.c
  3. **
  4. ** This example illustrates dbconvert().  It converts a 
  5. ** number of constants to strings, a number of strings 
  6. ** to numerical or binary quantities, and a number of
  7. ** numerical quantities to other numerical types.
  8. **
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <sybfront.h>
  13. #include <sybdb.h>
  14.  
  15. #define ARRAY_LEN      20
  16.  
  17. /* Forward declarations of the error handler and message handler. */
  18. int           err_handler();
  19. int           msg_handler();
  20.  
  21. main(argc, argv)
  22. int           argc;
  23. char          *argv[];
  24. {
  25.     /* These variables will hold the results of data conversions. */
  26.     static DBBINARY   my_binary_array[ARRAY_LEN]
  27.                        = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  28.     DBFLT8            my_flt8;
  29.     DBINT             my_int4;
  30.     DBMONEY           my_money;
  31.     DBCHAR            my_string[ARRAY_LEN];
  32.  
  33.     /* Initialize DB-Library. */
  34.     if (dbinit() == FAIL)
  35.         exit(ERREXIT);
  36.  
  37.     /* Install the user-supplied error-handling and message-handling
  38.      * routines. They are defined at the bottom of this source file.
  39.      */
  40.     dberrhandle(err_handler);
  41.     dbmsghandle(msg_handler);
  42.  
  43.     /* Convert numerical and binary constants to strings. */
  44.  
  45.     dbconvert
  46.         ((DBPROCESS *)NULL,
  47.          SYBBINARY, my_binary_array, (DBINT)8, SYBCHAR, my_string, (DBINT)-1);
  48.     printf("Binary constant 0x123456789abcdef0 converted to string "); 
  49.     printf("\"%s\".\n\n", my_string);
  50.  
  51.     my_flt8 = 55.555;
  52.     dbconvert
  53.      ((DBPROCESS *)NULL, SYBFLT8, &my_flt8, (DBINT)-1, SYBCHAR, my_string, 
  54.       (DBINT)-1);
  55.     printf
  56.      ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
  57.       my_string);
  58.  
  59.     /* Convert string constants to numerical and binary quantities. */
  60.  
  61.     dbconvert
  62.         ((DBPROCESS *)NULL, SYBCHAR, "123", (DBINT)-1, SYBINT4, &my_int4, 
  63.          (DBINT)-1);
  64.     printf
  65.         ("String constant \"123\" converted to 4-byte integer %ld.\n\n",
  66.          my_int4);
  67.  
  68.     dbconvert
  69.         ((DBPROCESS *)NULL, SYBCHAR, "0xfedc", (DBINT)-1, SYBBINARY, 
  70.          my_binary_array, (DBINT)ARRAY_LEN);
  71.     printf("String constant \"0xfedc\" converted to binary sequence ");
  72.     printf("%x.\n\n", *((int *)my_binary_array));
  73.  
  74.     dbconvert
  75.         ((DBPROCESS *)NULL, SYBCHAR, "123.456", (DBINT)-1, SYBFLT8, 
  76.          &my_flt8, (DBINT)-1);
  77.     printf("String constant \"123.456\" converted to ");
  78.     printf("floating-pt number %f.\n\n", my_flt8);
  79.  
  80.     /* Convert numerical types to other numerical types. */
  81.  
  82.     my_flt8 = 98.76;
  83.     dbconvert
  84.         ((DBPROCESS *)NULL, SYBFLT8, &my_flt8, (DBINT)-1, SYBMONEY, 
  85.          &my_money, (DBINT)-1);
  86.     dbconvert
  87.         ((DBPROCESS *)NULL, SYBMONEY, &my_money, (DBINT)-1, SYBCHAR, 
  88.          my_string, (DBINT)-1);
  89.     printf
  90.         ("floating-pt number %f converted to money value %s.\n\n",
  91.          my_flt8, my_string);
  92.  
  93.     dbconvert
  94.         ((DBPROCESS *)NULL, SYBMONEY, &my_money, (DBINT)-1, SYBFLT8, 
  95.          &my_flt8, (DBINT)-1);
  96.     printf
  97.         ("money value %s converted to floating-pt value %f.\n\n",
  98.          my_string, my_flt8);
  99.  
  100.     exit(STDEXIT);
  101.     dbexit();
  102. }
  103.  
  104. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  105. DBPROCESS       *dbproc;
  106. int             severity;
  107. int             dberr;
  108. int             oserr;
  109. char            *dberrstr;
  110. char            *oserrstr;
  111. {
  112.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  113.         return(INT_EXIT);
  114.     else 
  115.     {
  116.         printf("DB-Library error:\n\t%s\n", dberrstr);
  117.  
  118.         if (oserr != DBNOERR)
  119.             printf("Operating-system error:\n\t%s\n", oserrstr);
  120.  
  121.         return(INT_CANCEL);
  122.     }
  123. }
  124.  
  125. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  126.                 srvname, procname, line)
  127.  
  128. DBPROCESS       *dbproc;
  129. DBINT           msgno;
  130. int             msgstate;
  131. int             severity;
  132. char            *msgtext;
  133. char            *srvname;
  134. char            *procname;
  135. DBUSMALLINT     line;
  136.  
  137. {
  138.     printf ("Msg %ld, Level %d, State %d\n", 
  139.             msgno, severity, msgstate);
  140.  
  141.     if (strlen(srvname) > 0)
  142.         printf ("Server '%s', ", srvname);
  143.     if (strlen(procname) > 0)
  144.         printf ("Procedure '%s', ", procname);
  145.     if (line > 0)
  146.         printf ("Line %d", line);
  147.  
  148.     printf("\n\t%s\n", msgtext);
  149.  
  150.     return(0);
  151. }
  152.